package com.google.code.ptrends.entities;

import java.util.ArrayList;

public class ItemValueCollection extends ArrayList<ItemValueRecord> {

	private static final long serialVersionUID = 1L;
	private int itemID;
	private int fHashCode;

	public void setItemID(final int itemID) {
		this.itemID = itemID;
	}

	public int getItemID() {
		return itemID;
	}

	@Override
	public int hashCode() {
		if (fHashCode == 0) {
			fHashCode = 7 + 11 * itemID;
		}
		return fHashCode;
	}

	@Override
	public boolean equals(final Object arg0) {
		if (arg0 == null) {
			return false;
		}
		if (!(arg0 instanceof ItemValueCollection)) {
			return false;
		}
		ItemValueCollection obj = (ItemValueCollection) arg0;
		if (itemID != obj.getItemID())
			return false;
		if (this.size() != obj.size())
			return false;
		int pid;
		for (int i = 0; i < this.size(); i++) {
			if (obj.haveParameter(pid = ((ItemValueRecord) this.get(i))
					.getParametrID())) {
				if (!obj.getItemValueRecord(pid).getValue()
						.equals(this.getItemValueRecord(pid).getValue())) {
					return false;
				}
			} else
				return false;
		}
		return true;
	}

	public boolean haveParameter(final int parameterID) {
		for (ItemValueRecord ivr : this) {
			if (ivr.getParametrID() == parameterID) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @param parameterID
	 * @return
	 * @author Daniel
	 */
	public ItemValueRecord getItemValueRecord(final int parameterID) {
		for (ItemValueRecord ivr : this) {
			if (ivr.getParametrID() == parameterID) {
				return ivr;
			}
		}
		return null;
	}

}
